package com.example.yuejian23.yuejian_23.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.yuejian23.yuejian_23.R;
import com.example.yuejian23.yuejian_23.javaBean.LoginBean;
import com.example.yuejian23.yuejian_23.javaBean.RegBean;
import com.example.yuejian23.yuejian_23.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by dong on 2017/9/27.
 */

public class RegActivity extends AppCompatActivity {
    private EditText username;
    private EditText userpass;
    private EditText num;

    private Button reg;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        initView();
        initEvents();


    }

    private void initView() {
        num = (EditText) findViewById(R.id.editText);
        username = (EditText) findViewById(R.id.editText1);
        userpass = (EditText) findViewById(R.id.editText2);
        reg = (Button) findViewById(R.id.button1);
        //et_username=(EditText)findViewById(R.id.editText);
    }

    private void initEvents() {
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserModel userModel=new UserModel();
                Call call=userModel.reg(username.getText().toString(),num.getText().toString(),userpass.getText().toString());
                call.enqueue(new Callback<RegBean>(){
                    @Override
                    public void onResponse(Call<RegBean> call, Response<RegBean> response) {
                        //String success=response.body().getCode();
                        int userid = response.body().getCode();
                        if (userid == 0){
                            Toast.makeText(getApplication(),"注册失败", Toast.LENGTH_SHORT).show();
                            //失败处理"0".equals(success)
                        }else{

                            String str_num=num.getText().toString();
                            Intent intent=new Intent();
                            intent.putExtra("num",str_num);
                            setResult(RESULT_OK,intent);
                            Toast.makeText(getApplication(),"注册成功",Toast.LENGTH_SHORT).show();
                            finish();
                            //成功处理
                        }
                    }

                    @Override
                    public void onFailure(Call<RegBean> call, Throwable t) {

                    }
                });
            }

        });
    }
}
